/**
 * 
 */
package com.wgo.precise.client.ui.view.editor.properties;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.SectionPart;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.ui.views.properties.PropertySheetEntry;

import com.wgo.precise.client.ui.controller.listeners.IModelChangeListener;
import com.wgo.precise.client.ui.model.ClientModelItem;
import com.wgo.precise.client.ui.model.facade.IModelPersistable;
import com.wgo.precise.client.ui.model.facade.INodeParent;
import com.wgo.precise.client.ui.view.editor.IModelFormPart;
import com.wgo.precise.client.ui.view.editor.IViewerSectionPart;
import com.wgo.precise.client.ui.view.util.ModelItemPropertySource;


/**
 * @author petterei
 *
 * @version $Id: PropertiesSectionPart.java,v 1.2 2006-01-30 21:52:33 petterei Exp $
 */
public class PropertiesSectionPart extends SectionPart implements IModelFormPart, IModelChangeListener, IViewerSectionPart {

	public final static int MARGIN_HEIGHT = 3; 
	private DetailsSheetViewer viewer = null;
	private ClientModelItem item;
	
	public PropertiesSectionPart(PropertiesFormPage page, Composite parent) {
		this(page.getManagedForm().getToolkit(), parent);
	}
	public PropertiesSectionPart(FormToolkit toolkit,  Composite parent) {
		this(toolkit, parent, false);
	}
	public PropertiesSectionPart(PropertiesFormPage page, Composite parent, boolean twistie) {
		this(page.getManagedForm().getToolkit(), parent, twistie);
	}
	public PropertiesSectionPart(PropertiesFormPage page, Composite parent, boolean twistie, ClientModelItem item) {
		this(page.getManagedForm().getToolkit(), parent, twistie);
		this.item = item;
	}
	public PropertiesSectionPart(FormToolkit toolkit,  Composite parent, boolean twistie) {
		super(toolkit.createSection(parent, twistie ? Section.DESCRIPTION |ExpandableComposite.TITLE_BAR | ExpandableComposite.TWISTIE | ExpandableComposite.EXPANDED : Section.DESCRIPTION | ExpandableComposite.TITLE_BAR));
	}

	/**
	 * @see org.eclipse.ui.forms.AbstractFormPart#initialize(org.eclipse.ui.forms.IManagedForm)
	 */
	@Override
	public void initialize(IManagedForm form) {
		super.initialize(form);
		createContents(getSection(), getManagedForm().getToolkit());
	}

	/**
	 * @see org.eclipse.ui.forms.AbstractFormPart#setFormInput(java.lang.Object)
	 */
	@Override
	public boolean setFormInput(Object input) {
		boolean inputOk = super.setFormInput(input);
		if (input instanceof IAdaptable ) {
			IAdaptable adaptable = (IAdaptable)input;
			Object object = adaptable.getAdapter(IPropertySource.class);
			if (object instanceof ModelItemPropertySource) {
			     viewer.setInput(new Object[]{input});
			     inputOk = true;
			}
		}
		return inputOk;
	}

	private void createContents(Section section, FormToolkit toolkit) {
		section.setText("Properties");
		section.setDescription("Click on the cells to edit the values."); 
		section.marginHeight = MARGIN_HEIGHT;

		Composite detailsclient = toolkit.createComposite(section);											
		FillLayout clientLayout = new FillLayout();
		clientLayout.marginWidth = IModelFormPart.BORDER_SIZE;
		clientLayout.marginHeight = IModelFormPart.BORDER_SIZE;
		detailsclient.setLayout(clientLayout);
//		detailsclient.setLayout(new ViewerSectionLayout(this));
		section.setClient(detailsclient);

		viewer = new DetailsSheetViewer(detailsclient, section, getManagedForm());
		viewer.setClientModelItem(item);
		viewer.setSorter(new ExtendedPropertySheetSorter());
		PropertySheetEntry rootEntry = new PropertySheetEntry();
		rootEntry.setPropertySourceProvider(new ExtendedPropertySourceProvider());
		viewer.setRootEntry(rootEntry);

	    toolkit.paintBordersFor(detailsclient);
	    toolkit.adapt(viewer.getControl(), true, true);
	    ((Tree)viewer.getControl()).setLinesVisible(true); 
	}
	
	/**
	 * @see org.eclipse.ui.forms.AbstractFormPart#refresh()
	 */
	@Override
	public void refresh() {
		viewer.refresh();
		super.refresh();
	}

	/**
	 * @see com.wgo.precise.client.ui.view.facade.IModelUpdateListener#onModelUpdate(IModelPersistable)
	 */
	public void onModelUpdate(IModelPersistable item) {
		refresh();
	}

	@Override
	public void dispose() {
		viewer.getControl().dispose(); //?
		super.dispose();
	}

	/**
	 * @see com.wgo.precise.client.ui.view.editor.IViewerSectionPart#getViewer()
	 */
	public Viewer getViewer() {
		return viewer;
	}
	/* (non-Javadoc)
	 * @see com.wgo.precise.client.ui.view.facade.IModelChangeListener#onModelChange(com.wgo.precise.client.ui.model.ILabelProviderItem)
	 */
	public void onModelChange(IModelPersistable targetChangeItem) {
		refresh();
	}
	/**
	 * @see com.wgo.precise.client.ui.view.facade.IModelChangeListener#onModelItemDeleted(IModelPersistable, INodeParent)
	 */
	public void onModelItemDeleted(IModelPersistable deletedItem, INodeParent parent) {
	}
	/* (non-Javadoc)
	 * @see com.wgo.precise.client.ui.view.facade.IModelChangeListener#onNewModelItemCreated(com.wgo.precise.client.ui.model.ILabelProviderItem)
	 */
	public void onNewModelItemCreated(IModelPersistable createdItem) {
	}
	/* (non-Javadoc)
	 * @see org.eclipse.ui.forms.SectionPart#setFocus()
	 */
	@Override
	public void setFocus() {
		refresh();
		super.setFocus();
	}
	
}